package com.hontlong.aixue.bean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

public class User implements UserDetails{
	
	private static final long serialVersionUID = 1L;
	private int id;
	private String username;
	private String password;
	private boolean enabled=true;
	private Date indb;
	
	//Role-Authority
	private Set<Role> roles;
	
	public Collection<GrantedAuthority> getAuthorities() {
		if(roles==null || roles.isEmpty()){
			throw new IllegalStateException("roles must not is null or empty");
		}
		List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>(roles.size());
    	for(Role role : roles) {
    		grantedAuthorities.add(new GrantedAuthorityImpl(role.getRolename()));
    	}    	
        return grantedAuthorities;
	}

	public int getId() {
		return id;
	}

	public Date getIndb() {
		return indb;
	}

	public String getPassword() {
		return password;
	}
	
	public String getUsername() {
		return username;
	}

	public boolean isAccountNonExpired() {
		return true;
	}

	public boolean isAccountNonLocked() {
		return true;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setIndb(Date indb) {
		this.indb = indb;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}
